package com.keyinong.caizhengting.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.ImageUtils;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.keyinong.caizhengting.entity.ShangpinEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 商品适配器
 */

public class ShangpinAdapter extends BaseAdapter {
    Context context;
    ArrayList<ShangpinEntity> shangpinList;
    HttpTools httpTools = null;

    public ShangpinAdapter(Context context, ArrayList<ShangpinEntity> shangpinList) {
        super();
        this.context = context;
        this.shangpinList = shangpinList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return shangpinList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return shangpinList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHandle handle;
        if (view == null) {
            handle = new ViewHandle();
            view = LayoutInflater.from(context).inflate(R.layout.item_shangpin, null);
            handle.iv_shangpin = (ImageView) view.findViewById(R.id.imageView1);
            handle.tv_shangpinming = (TextView) view.findViewById(R.id.tv_shangpinming);
            handle.tv_content = (TextView) view.findViewById(R.id.tv_content);
            handle.tv_price = (TextView) view.findViewById(R.id.tv_price);
            handle.ib_shoucang1 = (ImageButton) view.findViewById(R.id.ib_shoucang1);
            handle.ib_shoucang2 = (ImageButton) view.findViewById(R.id.ib_shoucang2);
            handle.ib_add = (ImageButton) view.findViewById(R.id.ib_add);
            handle.ib_less = (ImageButton) view.findViewById(R.id.ib_less);
//			handle.ib_gouma=(ImageButton)view.findViewById(R.id.ib_gouwuche);
            handle.et_amout = (EditText) view.findViewById(R.id.et_amount);
            view.setTag(handle);
        } else {
            handle = (ViewHandle) view.getTag();
        }
        handle.tv_shangpinming.setText(shangpinList.get(position).getName());
        handle.tv_content.setText(shangpinList.get(position).getContent());
        handle.tv_price.setText(shangpinList.get(position).getPrice());
        handle.et_amout.setText(shangpinList.get(position).getAmount());
        ImageUtils.displayImage(context,
                (shangpinList.get(position).getImage()).replace("w/60", "w/120"),
                handle.iv_shangpin, R.drawable.wupian1);
        if (shangpinList.get(position).getCollect().equals("0")) {
            handle.ib_shoucang1.setVisibility(View.VISIBLE);
            handle.ib_shoucang2.setVisibility(View.INVISIBLE);
        } else {
            handle.ib_shoucang2.setVisibility(View.VISIBLE);
            handle.ib_shoucang1.setVisibility(View.INVISIBLE);
        }
        /**未收藏*/
        handle.ib_shoucang1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = context.getSharedPreferences("spReg",
                        Context.MODE_PRIVATE);
                String token = sp.getString("data", "");
                if (httpTools == null) {
                    httpTools = new HttpTools(context);
                }
                httpTools.addcollect(token, shangpinList.get(position).getId(),
                        new JsonHttpResponseHandler() {

                            @Override
                            public void onSuccess(int statusCode, Header[] headers,
                                                  JSONObject response) {
                                super.onSuccess(statusCode, headers, response);
                                try {
                                    boolean code = response.getBoolean("code");
                                    String msg = response.getString("msg");
                                    if (code == true) {
                                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                        handle.ib_shoucang1.setVisibility(View.INVISIBLE);
                                        handle.ib_shoucang2.setVisibility(View.VISIBLE);
//										view.setBackgroundResource(R.drawable.shouchang1);
                                    } else {
                                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }

                        });
            }
        });

        /**已收藏*/
        handle.ib_shoucang2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = context.getSharedPreferences("spReg",
                        Context.MODE_PRIVATE);
                String token = sp.getString("data", "");
                if (httpTools == null) {
                    httpTools = new HttpTools(context);
                }
                httpTools.cancelcollect(token, shangpinList.get(position).getId(),
                        new JsonHttpResponseHandler() {

                            @Override
                            public void onSuccess(int statusCode, Header[] headers,
                                                  JSONObject response) {
                                super.onSuccess(statusCode, headers, response);
                                try {
                                    boolean code = response.getBoolean("code");
                                    String msg = response.getString("msg");
                                    if (code == true) {
                                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                        handle.ib_shoucang2.setVisibility(View.INVISIBLE);
                                        handle.ib_shoucang1.setVisibility(View.VISIBLE);
//										view.setBackgroundResource(R.drawable.shouchang1);
                                    } else {
                                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }

                        });
            }
        });
        handle.ib_add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if ("".equals(handle.et_amout.getText().toString())) {
                    handle.et_amout.setText(0 + "");
                }
                int goodsNum = Integer.parseInt(handle.et_amout
                        .getText().toString());
                goodsNum++;
                handle.et_amout.setText(goodsNum + "");

            }
        });
        handle.ib_less.setOnClickListener(new OnClickListener() {
            int goodsNum;

            @Override
            public void onClick(View v) {
                if ("".equals(handle.et_amout.getText().toString())) {
                    handle.et_amout.setText(0 + "");
                    goodsNum = 0;
                } else {
                    goodsNum = Integer.parseInt(handle.et_amout
                            .getText().toString());
                }
                if (goodsNum > 0) {
                    goodsNum--;
                }
                handle.et_amout.setText(goodsNum + "");

            }
        });
        handle.ib_gouma.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (NetWorkUtil.isNetwork(context) == false) {
                    Toast.makeText(context, "当前网络不可用", 1).show();
                    return;
                }
                addgoodscat(position, handle.et_amout);
            }


        });
        return view;
    }

    private void addgoodscat(int index, EditText editText) {
        if (Integer.parseInt(editText.getText().toString()) <= 0) {
            Toast.makeText(context, "数量必须大于0", 1).show();
            return;
        }
        SharedPreferences sp = context.getSharedPreferences("spReg", Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        Log.i("info", "token" + "token" + token);
        if (httpTools == null) {
            httpTools = new HttpTools(context);
        }
        httpTools.addgoodscat(token, shangpinList.get(index).getId(), editText
                .getText().toString(), new JsonHttpResponseHandler() {
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                // TODO Auto-generated method stub
                super.onSuccess(statusCode, headers, response);
                try {
                    //							Log.i("zq",response.toString());
                    boolean code = response.getBoolean("code");
                    String msg = response.getString("msg");
                    if (code == true) {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    class ViewHandle {
        /**
         * 图片
         */
        ImageView iv_shangpin;
        /**
         * 商品名
         */
        TextView tv_shangpinming;
        /**
         * 说明
         */
        TextView tv_content;
        /**
         * 价格
         */
        TextView tv_price;
        /**
         * 未收藏
         */
        ImageButton ib_shoucang1;
        /**
         * 已收藏
         */
        ImageButton ib_shoucang2;
        /**
         * 加
         */
        ImageButton ib_add;
        /**
         * 减
         */
        ImageButton ib_less;
        /**
         * 购买
         */
        ImageButton ib_gouma;
        /**
         * 数量
         */
        EditText et_amout;
    }
}


